package walk

import (
	"fmt"
	"gitee.com/snxamdf/wui/config"
	"gitee.com/snxamdf/wui/logs"
	"gitee.com/snxamdf/wui/tools"
	"github.com/lxn/walk"
	"github.com/lxn/walk/declarative"
	"github.com/lxn/win"
	"strconv"
	"strings"
	"time"
)

const (
	width  = 800
	height = 600
)

type WebGUI struct {
	Title    string
	IndexUrl string
}

func check(app *WebGUI) {
	if app.IndexUrl == "" {
		panic("未设置 WebGUI IndexUrl 必备参数")
	}
	if app.Title == "" {
		app.Title = "WUI窗口"
	}
}

func NewWalkUI(app *WebGUI) {
	check(app)
	indexData := data{
		WebGUI:      *app,
		randomIndex: util.MD5(strconv.Itoa(time.Now().Second())),
		RootPath:    fmt.Sprintf("/%s", config.GetConfig("assets_path")),
	}
	indexServer(indexData)
	var le *walk.LineEdit
	var wv *walk.WebView
	var url = fmt.Sprintf("http://%s/%s", config.GetConfig("addr"), indexData.randomIndex)
	var windowMain *walk.MainWindow
	err := declarative.MainWindow{
		Icon:     declarative.Bind("'favicon.ico'"),
		Title:    app.Title,
		Size:     declarative.Size{width, height},
		Layout:   declarative.VBox{MarginsZero: true},
		AssignTo: &windowMain,
		Children: []declarative.Widget{
			declarative.LineEdit{
				AssignTo: &le,
				Text:     declarative.Bind("wv.URL"),
				OnKeyDown: func(key walk.Key) {
					if key == walk.KeyReturn {
						if err := wv.SetURL(le.Text()); err != nil {
							logs.Info(err)
						}
					}
				},
			},
			declarative.WebView{
				Font: declarative.Font{
					PointSize: 12,
					Family:    "Msyhl",
				},
				AssignTo: &wv,
				Name:     "wv",
				URL:      url,
				//ShortcutsEnabled:         false,
				//NativeContextMenuEnabled: false,
				ShortcutsEnabled:         true,
				NativeContextMenuEnabled: true,
				OnURLChanged: func() {
					logs.Info("OnURLChanged")
				},
				OnWindowClosing: func(eventData *walk.WebViewWindowClosingEventData) {
					logs.Info("OnWindowClosing")
				},
			},
		},
		Functions: map[string]func(args ...interface{}) (interface{}, error){
			"icon": func(args ...interface{}) (interface{}, error) {
				//logs.Info(args)
				if strings.HasPrefix(args[0].(string), "https") {
					return "check", nil
				}
				return "stop", nil
			},
		},
	}.Create()
	if err != nil {
		logs.Info("create WGUI Error ->", err)
		return
	}
	//win.SetWindowLong(
	//	windowMain.Handle(), win.GWL_STYLE,
	//	win.GetWindowLong(windowMain.Handle(), win.GWL_STYLE) & win.WS_DLGFRAME   ,
	//)
	// 设置 ^win.WS_MAXIMIZEBOX 禁用最大化按钮
	// 设置 ^win.WS_THICKFRAME 禁用窗口大小改变
	win.SetWindowLong(
		windowMain.Handle(), win.GWL_STYLE,
		win.GetWindowLong(windowMain.Handle(), win.GWL_STYLE) & ^win.WS_MAXIMIZEBOX & ^win.WS_THICKFRAME,
	)
	//设置居中显示
	xScreen := win.GetSystemMetrics(win.SM_CXSCREEN)
	yScreen := win.GetSystemMetrics(win.SM_CYSCREEN)
	win.SetWindowPos(
		windowMain.Handle(),
		0,
		(xScreen-width)/2,
		(yScreen-height)/2,
		width,
		height,
		win.SWP_NOSIZE,
	)
	win.ShowWindow(windowMain.Handle(), win.SW_SHOW)
	r := windowMain.Run()
	logs.Info("run result ->", r)
}

func WebViewGUI() {
	logs.Info("GUI")
	var le *walk.LineEdit
	var wv *walk.WebView
	var url = fmt.Sprintf("http://%s", config.GetConfig("addr"))
	//url="https://www.jq22.com/yanshi546"
	//url = "http://www.miniui.com/demo/"
	//url = fmt.Sprintf("http://%s/example/example.html", GetConfig("addr"))
	//url = fmt.Sprintf("http://%s/demo/sockjs.html", GetConfig("addr"))
	//url = fmt.Sprintf("http://%s/demo/way.html", GetConfig("addr"))
	var windowMain *walk.MainWindow
	declarative.MainWindow{
		Icon:     declarative.Bind("'favicon.ico'"),
		Title:    "脚本替换器",
		Size:     declarative.Size{width, height},
		Layout:   declarative.VBox{MarginsZero: true},
		AssignTo: &windowMain,
		Children: []declarative.Widget{
			declarative.LineEdit{
				AssignTo: &le,
				Text:     declarative.Bind("wv.URL"),
				OnKeyDown: func(key walk.Key) {
					if key == walk.KeyReturn {
						if err := wv.SetURL(le.Text()); err != nil {
							logs.Info(err)
						}
					}
				},
			},
			declarative.WebView{
				Font: declarative.Font{
					PointSize: 12,
					Family:    "Msyhl",
				},
				AssignTo: &wv,
				Name:     "wv",
				URL:      url,
				//ShortcutsEnabled:         false,
				//NativeContextMenuEnabled: false,
				ShortcutsEnabled:         true,
				NativeContextMenuEnabled: true,
				OnURLChanged: func() {
					logs.Info("OnURLChanged")
				},
				OnWindowClosing: func(eventData *walk.WebViewWindowClosingEventData) {
					logs.Info("OnWindowClosing")
				},
			},
		},
		Functions: map[string]func(args ...interface{}) (interface{}, error){
			"icon": func(args ...interface{}) (interface{}, error) {
				//logs.Info(args)
				if strings.HasPrefix(args[0].(string), "https") {
					return "check", nil
				}
				return "stop", nil
			},
		},
	}.Create()
	// 设置 ^win.WS_MAXIMIZEBOX 禁用最大化按钮
	// 设置 ^win.WS_THICKFRAME 禁用窗口大小改变
	win.SetWindowLong(
		windowMain.Handle(), win.GWL_STYLE,
		win.GetWindowLong(windowMain.Handle(), win.GWL_STYLE) & ^win.WS_MAXIMIZEBOX & ^win.WS_THICKFRAME,
	)
	win.SetWindowLong(
		windowMain.Handle(), win.WS_VSCROLL,
		win.GetWindowLong(windowMain.Handle(), win.GWL_STYLE) & ^win.WS_VSCROLL,
	)
	//设置居中显示
	xScreen := win.GetSystemMetrics(win.SM_CXSCREEN)
	yScreen := win.GetSystemMetrics(win.SM_CYSCREEN)
	win.SetWindowPos(
		windowMain.Handle(),
		0,
		(xScreen-width)/2,
		(yScreen-height)/2,
		width,
		height,
		win.SWP_FRAMECHANGED,
	)
	win.ShowWindow(windowMain.Handle(), win.SW_SHOW)
	r := windowMain.Run()
	logs.Info(r)
}
